//
//  UITextView+zb.m
//  BaseRACDemo
//
//  Created by 庄波 on 2018/3/30.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "UITextView+zb.h"

@implementation UITextView (zb)
- (void)contentSizeToFit:(UIFont*)font {
    if([self.text length]>0) {
        CGSize contentSize = self.contentSize;
        //NSLog(@"w:%f h%f",contentSize.width,contentSize.height);
        UIEdgeInsets offset;
        CGSize newSize = contentSize;
        if(contentSize.height <= self.frame.size.height) {
            CGFloat offsetY = (self.frame.size.height - contentSize.height)/2;
            offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
        }
        else {
            newSize = self.frame.size;
            offset = UIEdgeInsetsZero;
            CGFloat fontSize = 21;
            while (contentSize.height > self.frame.size.height) {
                [self setFont:[UIFont fontWithName:@"Helvetica Neue" size:fontSize--]];
                contentSize = self.contentSize;
            }
            newSize = contentSize;
        }
        [self setContentSize:newSize];
        [self setContentInset:offset];
    }
}
@end
